package com.yanqu.road.dao.impl.activity.commonteam;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamData;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonTeamUserDataDaoImpl extends TempDataObjectDao<CommonTeamUserData> {

    public static String getCommonTeamUserDataSql = "select * from `t_c_common_team_user` where `activityId` = ? and groupId = ?";
    public Map<Long, CommonTeamUserData> getCommonTeamUserDataMap(int activityId, int groupId) {
        Map<Long, CommonTeamUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCommonTeamUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonTeamUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCommonTeamUserDataSql = "update `t_c_common_team_user` set teamId=?,userName=?,serverId=?,score=?,lastJoinTeamTime=?,lastCreateTeamTime=?,selfLeaveTeamTime=?,createTime=?,extendsParam=? where activityId=? and groupId = ? and userId = ?";
    @Override
    protected boolean updateData(CommonTeamUserData commonTeamUserData) {
        commonTeamUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getTeamId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamUserData.getUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getLastJoinTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getLastCreateTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getSelfLeaveTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamUserData.getExtendsParam()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCommonTeamUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonTeamUserDataSql = "insert into `t_c_common_team_user`(activityId,groupId,teamId,userId,userName,serverId,score,lastJoinTeamTime,lastCreateTeamTime,selfLeaveTeamTime,createTime,extendsParam) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CommonTeamUserData commonTeamUserData) {
        commonTeamUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamUserData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamUserData.getUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getLastJoinTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getLastCreateTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getSelfLeaveTeamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamUserData.getExtendsParam()));
        if (getDb().executeNoneQuery(insertCommonTeamUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonTeamUserData parseData(ResultSet rs) throws SQLException {
        CommonTeamUserData commonTeamUserData = new CommonTeamUserData();
        commonTeamUserData.setActivityId(rs.getInt("activityId"));
        commonTeamUserData.setGroupId(rs.getInt("groupId"));
        commonTeamUserData.setTeamId(rs.getInt("teamId"));
        commonTeamUserData.setUserId(rs.getLong("userId"));
        commonTeamUserData.setUserName(rs.getString("userName"));
        commonTeamUserData.setServerId(rs.getLong("serverId"));
        commonTeamUserData.setScore(rs.getLong("score"));
        commonTeamUserData.setLastJoinTeamTime(rs.getLong("lastJoinTeamTime"));
        commonTeamUserData.setLastCreateTeamTime(rs.getLong("lastCreateTeamTime"));
        commonTeamUserData.setSelfLeaveTeamTime(rs.getLong("selfLeaveTeamTime"));
        commonTeamUserData.setCreateTime(rs.getLong("createTime"));
        commonTeamUserData.setExtendsParam(rs.getString("extendsParam"));
        return commonTeamUserData;
    }
}
